package com.zhw.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhw.Util.JwtUtils;
import com.zhw.mapper.CustomerMapper;
import com.zhw.mapper.ServeMapper;
import com.zhw.mapper.UserMapper;
import com.zhw.pojo.PageBean;
import com.zhw.pojo.dto.ServeQueryDTO;
import com.zhw.pojo.entry.CustomerServe;
import com.zhw.pojo.entry.User;
import com.zhw.service.CustomerServeService;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CustomerServeServiceImpl implements CustomerServeService {

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    ServeMapper serveMapper;


    @Autowired
    UserMapper userMapper;


    @Autowired
    HttpServletRequest request;


    @Override
    public PageBean page(ServeQueryDTO serveQueryDTO) {
        PageHelper.startPage(serveQueryDTO.getPage(), serveQueryDTO.getPageSize());
        if(serveQueryDTO.getCustomer()!=null && serveQueryDTO.getCustomer()!=""){
            String name = customerMapper.getNameByID(Integer.parseInt(serveQueryDTO.getCustomer()));
            serveQueryDTO.setCustomer(name);
        }

        Page<CustomerServe> page = serveMapper.page(serveQueryDTO);;



        for (int i = 0; i < page.size(); i++) {
            if (page.get(i).getAssigner()!=null && page.get(i).getAssigner()!="" ){
                User user = userMapper.getByID(Long.valueOf(page.get(i).getAssigner()));
                page.get(i).setAssigner(user.getUsername());
            }
        }







        return new PageBean(page.getTotal(), page.getResult());


    }

    @Override
    public void save(CustomerServe customerServe) {
        customerServe.setCustomer(customerMapper.getNameByID(Integer.parseInt(customerServe.getCustomer())));

        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        customerServe.setCreatePeople((String) claims.get("username"));
        customerServe.setUpdateDate(LocalDateTime.now());
        customerServe.setCreateDate(LocalDateTime.now());
        customerServe.setIsValid(1);
        customerServe.setStatus(1);

        serveMapper.save(customerServe);
    }

    @Override
    public CustomerServe getServeByIdAndStatus(Integer id, Integer status) {
        CustomerServe customerServe = serveMapper.getServeByIdAndStatus(id, status);
        if (customerServe.getAssigner()!=null&&customerServe.getAssigner()!=""){
            User user = userMapper.getByID(Long.valueOf(customerServe.getAssigner()));
            customerServe.setAssigner(user.getUsername());
        }
        return customerServe;
    }


    @Override
    public void assignment(CustomerServe customerServe) {
        customerServe.setAssignTime(LocalDateTime.now());
        customerServe.setUpdateDate(LocalDateTime.now());
        customerServe.setStatus(2);

        if (customerServe.getAssigner()!=null&&customerServe.getAssigner()!=""){
            User u = userMapper.getByUsername(customerServe.getAssigner());
            customerServe.setAssigner(u.getId());

        }

        serveMapper.update(customerServe);
    }


    @Override
    public PageBean getDisPose(ServeQueryDTO serveQueryDTO) {
        PageHelper.startPage(serveQueryDTO.getPage(),serveQueryDTO.getPageSize());
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String id= (String) claims.get("id");
        serveQueryDTO.setAssigner(id);
        Page<CustomerServe> page = serveMapper.page(serveQueryDTO);

        for (int i = 0; i < page.size(); i++) {
            if (page.get(i).getAssigner()!=null && page.get(i).getAssigner()!="" ){
                User user = userMapper.getByID(Long.valueOf(page.get(i).getAssigner()));
                page.get(i).setAssigner(user.getUsername());
            }

        }
        return new PageBean(page.getTotal(),page.getResult());


    }


    @Override
    public void dispose(CustomerServe customerServe) {
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String username = (String) claims.get("username");

        customerServe.setServiceProcePeople(username);
        customerServe.setServiceProceTime(LocalDateTime.now());
        customerServe.setUpdateDate(LocalDateTime.now());
        customerServe.setStatus(3);

        if (customerServe.getAssigner()!=null&&customerServe.getAssigner()!=""){
            User user = userMapper.getByUsername(username);
            customerServe.setAssigner(user.getId());
        }

        serveMapper.update(customerServe);
    }


    @Override
    public void feedback(CustomerServe customerServe) {
        customerServe.setMyd(getStar(Integer.valueOf(customerServe.getMyd())));
        customerServe.setUpdateDate(LocalDateTime.now());
        customerServe.setStatus(4);
        serveMapper.update(customerServe);
    }

    public String getStar(Integer number) {
        String[] stars = {"待考察", "☆", "☆☆", "☆☆☆", "☆☆☆☆", "☆☆☆☆☆"};
        return stars[number];
    }

}
